﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HMIS.Infrastructure.Core.Authority;
using HMIS.FrameWork;
using HMIS.MVC.CommonSupport;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.ViewModel;
using System.ComponentModel;

namespace HMIS.MVC.Infrastructure.Authority
{
	public class InfraMenuController : BaseController
	{
		private readonly IMenuFacade MenuFacade;
		public InfraMenuController(IMenuFacade MenuFacade)
		{
			this.MenuFacade = MenuFacade;
		}

		[Description("访问菜单管理页面")]
		[ViewPage]
		public ActionResult Menu()
		{
			return View();
		}

		public class MenuTree : ExtTreeData<MenuTree>
		{
			public string Name;

			public string TreeCode;

			public string ActionName { get; set; }

			public string ControllerName { get; set; }

			public string SmallIcon { get; set; }

			public string BigIcon { get; set; }

			public string URL { get; set; }

			public bool IsExtPage { get; set; }

			public bool IsFunction { get; set; }

			public string Description { get; set; }
		}

		[Description("获取菜单的数据源")]
		public ActionResult GetMenuGrid()
		{
			var data = MenuFacade.GetAlllist();
			return this.JsonFormat(
				new ExtGirdData<MenuNode> { total = 0, data = data });
		}

		[Description("获取菜单对照的action数据源")]
		public ActionResult GetActionQueryList()
		{
			long total;
			var data = MenuFacade.QueryActionPlist(
				Request["query"],
				int.Parse(Request["start"]),
				int.Parse(Request["limit"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<ActionPermission> { data = data, total = total });

		}

		[ExtResult]
		[Description("根据controllerName和actionName获取URL")]
		public ActionResult GetUrl()
		{
			string url = MenuNode.GetURL(Request["controllerName"], Request["actionName"]);
			return this.JsonFormat(url);
		}

		[ExtResult]
		[Description("添加、修改菜单数据")]
		public ActionResult SaveOrUpdate()
		{
			MenuNode entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new MenuNode
				{
					IsExtPage = Request["IsExtPage"] == null ? false : bool.Parse(Request["IsExtPage"]),
					Leaf = Request["Leaf"] == null ? false : bool.Parse(Request["Leaf"]),
					IsFunction = Request["IsFunction"] == null ? false : bool.Parse(Request["IsFunction"]),
					BigIcon = Request["BigIcon"],
					Level = int.Parse(Request["Level"]) + 1,
					ActionName = Request["ActionName"],
					ControllerName = Request["ControllerName"],
					Description = Request["Description"],
					Index = String.IsNullOrEmpty(Request["Index"]) ? 0 : int.Parse(Request["Index"]),
					Name = Request["Name"],
					ParentId = Request["ParentId"],
					SmallIcon = Request["SmallIcon"],
					TreeCode = Request["TreeCode"],
					URL = Request["URL"],
				};
			}
			else
			{
				entity = MenuFacade.Get(Request["Id"]);
				entity.IsExtPage = Request["IsExtPage"] == null ? false : bool.Parse(Request["IsExtPage"]);
				entity.Leaf = Request["Leaf"] == null ? false : bool.Parse(Request["Leaf"]);
				entity.IsFunction = Request["IsFunction"] == null ? false : bool.Parse(Request["IsFunction"]);
				entity.BigIcon = Request["BigIcon"];
				entity.Description = Request["Description"];
				entity.Index = String.IsNullOrEmpty(Request["Index"]) ? 0 : int.Parse(Request["Index"]);
				entity.SmallIcon = Request["SmallIcon"];
			}

			MenuFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("判断菜单的代码是否唯一")]
		public ActionResult IsFieldExits()
		{
			var result = MenuFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}

		[Description("根据ID获取菜单实体")]
		public ActionResult GetMenuById()
		{
			var entity = MenuFacade.Get(Request["Id"]);
			return this.JsonFormat(
				new ExtFormLoadData<MenuNode> { data = new MenuNode[] { entity } }
				);
		}

		[Description("根据用户ID获取关联菜单集合")]
		public ActionResult GetMenuListByUserId()
		{
			IList<MenuNode> menuNodes = MenuFacade.GetMenuListByUserId(Request["Id"]);
			return this.JsonFormat(
				new ExtGirdData<MenuNode> { total = 0, data = menuNodes });
		}

		[Description("根据用户ID获取所有关联菜单集合")]
		public ActionResult GetAllListByUserId()
		{
			IList<MenuNode> menuNodes = MenuFacade.GetAllListByUserId(Request["Id"]);
			return this.JsonFormat(
				new ExtGirdData<MenuNode> { total = 0, data = menuNodes });
		}

		//删除字典类
		[ExtResult]
		[Description("删除菜单实体")]
		public ActionResult DeleteDicCategory()
		{
			MenuFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

	}
}
